/**
 * 评论模型
 * 用于游戏评论和评分
 */

const mongoose = require('mongoose');

// 评论模型定义
const CommentSchema = new mongoose.Schema({
    game: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Game',
        required: [true, '请指定游戏']
    },
    user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: [true, '请指定用户']
    },
    content: {
        type: String,
        required: [true, '请输入评论内容'],
        trim: true,
        minlength: [3, '评论内容至少需要3个字符'],
        maxlength: [500, '评论内容不能超过500个字符']
    },
    rating: {
        type: Number,
        required: [true, '请给出评分'],
        min: [1, '评分最低为1'],
        max: [5, '评分最高为5']
    },
    likes: {
        type: Number,
        default: 0
    },
    createdAt: {
        type: Date,
        default: Date.now
    },
    updatedAt: {
        type: Date,
        default: Date.now
    }
});

// 创建索引
CommentSchema.index({ game: 1, user: 1 }, { unique: true });

// 更新时间中间件
CommentSchema.pre('save', function(next) {
    this.updatedAt = Date.now();
    next();
});

module.exports = mongoose.model('Comment', CommentSchema); 